package com.wxn.caincameracopy2

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.cgfay.camera.PreviewEngine
import com.cgfay.camera.fragment.NormalMediaSelector
import com.cgfay.camera.listener.OnPreviewCaptureListener
import com.cgfay.camera.model.AspectRatio
import com.cgfay.filter.glfilter.resource.FilterHelper
import com.cgfay.filter.glfilter.resource.MakeupHelper
import com.cgfay.filter.glfilter.resource.ResourceHelper
import com.cgfay.image.activity.ImageEditActivity
import com.cgfay.picker.MediaPicker
import com.cgfay.uitls.utils.PermissionUtils
import com.cgfay.video.activity.VideoEditActivity
import com.wxn.caincameracopy2.databinding.ActivityMainBinding
import com.wxn.caincameracopy2.record.SpeedRecordActivity

class MainActivity: AppCompatActivity() {

    companion object {
        const val REQUEST_CODE = 0
    }

    private var mHandler: Handler? = null

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(LayoutInflater.from(applicationContext))
        setContentView(binding.root)
        checkPermissions()
        if (PermissionUtils.permissionChecking(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            initResources()
        }
        mHandler = Handler()

        binding.btnCamera.setOnClickListener {
            previewCamera()
        }
        binding.btnEditVideo.setOnClickListener {
            scanMedia(false, true)

        }
        binding.btnEditPicture.setOnClickListener{
            scanMedia(true, false)
        }

        binding.btnSpeedRecord.setOnClickListener {
            val intent = Intent(this@MainActivity, SpeedRecordActivity::class.java)
            startActivity(intent)
        }
    }



    private fun checkPermissions() {
        PermissionUtils.requestPermissions(
            this, arrayOf<String>(
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO
            ), REQUEST_CODE
        )
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                initResources()
            }
        }
    }


    /**
     * 初始化动态贴纸、滤镜等资源
     */
    private fun initResources() {
        Thread {
            ResourceHelper.initAssetsResource(this@MainActivity)
            FilterHelper.initAssetsFilter(this@MainActivity)
            MakeupHelper.initAssetsMakeup(this@MainActivity)
        }.start()
    }

    /**
     * 打开预览页面
     */
    private fun previewCamera() {
        if (PermissionUtils.permissionChecking(this, Manifest.permission.CAMERA)) {
            PreviewEngine.from(this)
                .setCameraRatio(AspectRatio.Ratio_16_9)
                .showFacePoints(false)
                .showFps(true)
                .backCamera(true)
                .setPreviewCaptureListener { path: String?, type: Int ->
                    if (type == OnPreviewCaptureListener.MediaTypePicture) {
                        val intent = Intent(this@MainActivity, ImageEditActivity::class.java)
                        intent.putExtra(ImageEditActivity.IMAGE_PATH, path)
                        intent.putExtra(ImageEditActivity.DELETE_INPUT_FILE, true)
                        startActivity(intent)
                    } else if (type == OnPreviewCaptureListener.MediaTypeVideo) {
                        val intent = Intent(this@MainActivity, VideoEditActivity::class.java)
                        intent.putExtra(VideoEditActivity.VIDEO_PATH, path)
                        startActivity(intent)
                    }
                }
                .startPreview()
        } else {
            checkPermissions()
        }
    }


    /**
     * 扫描媒体库
     * @param enableImage
     * @param enableVideo
     */
    private fun scanMedia(enableImage: Boolean, enableVideo: Boolean) {
        MediaPicker.from(this)
            .showImage(enableImage)
            .showVideo(enableVideo)
            .setMediaSelector(NormalMediaSelector())
            .show()
    }
}